These are instructions on how to use the functions in the library psrc_census.R
The code requires several libraries. The config file contains global values specific to PSRC. The main code is psrc_census.R.
Currently there are two functions:
psrc_acs_table retrieves acs data for a variable and puts it in a table at a chosen geography, for a chosen year
create_tract_map makes a map of the region at the tract level for a variable
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.1 v dplyr 1.0.5
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(leaflet)
library(tidycensus)
## Warning: package 'tidycensus' was built under R version 4.0.5
library(writexl)
## Warning: package 'writexl' was built under R version 4.0.5
source('../library/psrc_census_config.R')
source('../library/psrc_census.R')
The first time you run this code, you will need to set our Census API Key as an environment variable, if you haven’t doen taht. After that you can just get it. This is the website to get a key: https://api.census.gov/data/key_signup.html. Once you run Sys.setenv on the Census API Key you will only need to run Sys.getenv.
#Sys.setenv(CENSUS_API_KEY = 'PUT YOUR KEY HERE')
Sys.getenv("CENSUS_API_KEY")
Next you need to decide what tables you would like to download. This is the hardest part because you have find the correct table code, decide on geography, and which years.
The main function psrc_acs_table has four parameters: tbl_code, geog, yr,acs.
tbl_code is the code of the ACS table list on the Census website, in string format, such as “B02001_005”
geog is the geography, currently with the options of “county” or “tract”
yr is the numeric year of the data, such as 2019
acs is which dataset you are using, such as “acs1” or “acs5”
You can find the list of ACS datasets available via the api here: https://www.census.gov/data/developers/data-sets.html #psrc_table(tbl_code, geog, yr,acs)
tract_asian<-psrc_acs_table("B02001_005", "tract", 2019,'acs5')
## Getting data from the 2015-2019 5-year ACS
## # A tibble: 776 x 10
## GEOID NAME variable estimate moe ACS_Year ACS_Type ACS_Geography label
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <chr>
## 1 53033~ Census~ B02001_~ 1202 248 2019 acs5 tract Estim~
## 2 53033~ Census~ B02001_~ 1168 232 2019 acs5 tract Estim~
## 3 53033~ Census~ B02001_~ 484 155 2019 acs5 tract Estim~
## 4 53033~ Census~ B02001_~ 970 418 2019 acs5 tract Estim~
## 5 53033~ Census~ B02001_~ 465 194 2019 acs5 tract Estim~
## 6 53033~ Census~ B02001_~ 278 143 2019 acs5 tract Estim~
## 7 53033~ Census~ B02001_~ 1094 355 2019 acs5 tract Estim~
## 8 53033~ Census~ B02001_~ 997 254 2019 acs5 tract Estim~
## 9 53033~ Census~ B02001_~ 412 195 2019 acs5 tract Estim~
## 10 53033~ Census~ B02001_~ 323 64 2019 acs5 tract Estim~
## # ... with 766 more rows, and 1 more variable: concept <chr>
county_ferry<-psrc_acs_table("B08006_013", "county", 2019, 'acs1')
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2019 1-year ACS
## # A tibble: 4 x 10
## GEOID NAME variable estimate moe ACS_Year ACS_Type ACS_Geography label
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <chr>
## 1 53033 King ~ B08006_~ 1687 854 2019 acs1 county Estimate~
## 2 53035 Kitsa~ B08006_~ 9788 2076 2019 acs1 county Estimate~
## 3 53053 Pierc~ B08006_~ 118 124 2019 acs1 county Estimate~
## 4 53061 Snoho~ B08006_~ 61 106 2019 acs1 county Estimate~
## # ... with 1 more variable: concept <chr>
region_female_under_5<-psrc_acs_table("B01001_027", "region", 2019, 'acs1')
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2019 1-year ACS
## # A tibble: 1 x 5
## variable total_region moe_region ACS_Year ACS_Type
## <chr> <dbl> <dbl> <dbl> <chr>
## 1 B01001_027 125373 1275. 2019 acs1
#if you want you can write the data to the clipboard or out to csv or excel
write.table(county_ferry, "clipboard", sep="\t", row.names=FALSE)
write.csv(county_ferry, "ferry_workers_by_county.csv")
write_xlsx(county_ferry, "ferry_workers_by_county.xlsx")
create_tract_map, we send in a data frame by tract, and it makes a map.
create_tract_map(tract_asian)
## Reading layer `dbo.tract2010_nowater' from data source `MSSQL:server=AWS-PROD-SQL\Sockeye;database=ElmerGeo;trusted_connection=yes' using driver `MSSQLSpatial'
## Simple feature collection with 773 features and 19 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 1099353 ymin: -97548.53 xmax: 1622631 ymax: 477101.5
## projected CRS: NAD83 / Washington North (ftUS)